%{
#include <stdio.h>
#include <string.h>

#include "parse_jobstream.h"

#define  SCANDBG(x)	fprintf(stderr, "%s\n", x)
#define  SCANDBG1(x,y)	fprintf(stderr, "%s<%s>\n", x, y)
#ifdef DEBUG
#define dbg_flag 1
#else
#define dbg_flag 0
#endif

int yylex();
YYSTYPE yylval;

%}

%option noyywrap nounput

%x      QSTRING

%%
[ \t\r\n\f]+		{ /* Skip the white spaces... */ }
^"#".*$			{ /* Skip shell-style single line comments... */ }
"*".*$			{ /* Skip '*' style single line comments... */ }
"<<"[^>]*">>"		{ /* Skip angular bracket comments... */ }
\"			{ /* Starting a quoted string... */
			  BEGIN(QSTRING);
			}
<QSTRING>([^"\\]*(\\.[^"\\]*)*)\" { /* Matching the quoted string... */
			  BEGIN(INITIAL); /* End the quoted (") string... */
			  yytext[yyleng-1]='\0'; yyleng--;
			  yylval.string = strdup(yytext);
			  SCANDBG1("TOK_QSTRING", yytext);
			  return TOK_QSTRING;
                        }
\!			{ SCANDBG("TOK_EXCLAIM"); return TOK_EXCLAIM; }
\#			{ SCANDBG("TOK_POUND"); return TOK_POUND; }
\+			{ SCANDBG("TOK_PLUS"); return TOK_PLUS; }
\:			{ SCANDBG("TOK_COLON"); return TOK_COLON; }
(\-)/([^s]|([s][^au]))	{ SCANDBG("TOK_MINUS"); return TOK_MINUS; }
"-sa"			{ SCANDBG("TOK_SATURDAY"); return TOK_SATURDAY; }
"-su"			{ SCANDBG("TOK_SUNDAY"); return TOK_SUNDAY; }
"at"			{ SCANDBG("TOK_AT"); return TOK_AT; }
"carryforward"		{ SCANDBG("TOK_CF"); return TOK_CF; }
"confirmed"		{ SCANDBG("TOK_CONFIRMED"); return TOK_CONFIRMED; }
"critical"		{ SCANDBG("TOK_CRITICAL"); return TOK_CRITICAL; }
"day"[s?]		{ SCANDBG("TOK_DAYS"); return TOK_DAYS; }
"deadline"		{ SCANDBG("TOK_DEADLINE"); return TOK_DEADLINE; }
"description"		{ SCANDBG("TOK_DESC"); return TOK_DESC; }
"draft"			{ SCANDBG("TOK_DRAFT"); return TOK_DRAFT; }
"end"			{ SCANDBG("TOK_END"); return TOK_END; }
"every"			{ SCANDBG("TOK_EVERY"); return TOK_EVERY; }
"except"		{ SCANDBG("TOK_EXCEPT"); return TOK_EXCEPT; }
"fdignore"		{ SCANDBG("TOK_FDIGNORE"); return TOK_FDIGNORE; }
"fdnext"		{ SCANDBG("TOK_FDNEXT"); return TOK_FDNEXT; }
"fdprev"		{ SCANDBG("TOK_FDPREV"); return TOK_FDPREV; }
"follows"		{ SCANDBG("TOK_FOLLOWS"); return TOK_FOLLOWS; }
"freedays"		{ SCANDBG("TOK_FREEDAYS"); return TOK_FREEDAYS; }
"from"			{ SCANDBG("TOK_FROM"); return TOK_FROM; }
"go"			{ SCANDBG("TOK_GO"); return TOK_GO; }
"hi"			{ SCANDBG("TOK_HI"); return TOK_HI; }
"keyjob"		{ SCANDBG("TOK_KEYJOB"); return TOK_KEYJOB; }
"keysched"		{ SCANDBG("TOK_KEYSCHED"); return TOK_KEYSCHED; }
"limit"			{ SCANDBG("TOK_LIMIT"); return TOK_LIMIT; }
"matching"		{ SCANDBG("TOK_MATCHING"); return TOK_MATCHING; }
"needs"			{ SCANDBG("TOK_NEEDS"); return TOK_NEEDS; }
"on"/([:space:]+)	{ SCANDBG("TOK_ON"); return TOK_ON; }
"onuntil"		{ SCANDBG("TOK_ONUNTIL"); return TOK_ONUNTIL; }
"opens"			{ SCANDBG("TOK_OPENS"); return TOK_OPENS; }
"previous"		{ SCANDBG("TOK_PREVIOUS"); return TOK_PREVIOUS; }
"priority"		{ SCANDBG("TOK_PRIORITY"); return TOK_PRIORITY; }
"prompt"		{ SCANDBG("TOK_PROMPT"); return TOK_PROMPT; }
"relative"		{ SCANDBG("TOK_RELATIVE"); return TOK_RELATIVE; }
"runcycle"		{ SCANDBG("TOK_RUNCYCLE"); return TOK_RUNCYCLE; }
"sameday"		{ SCANDBG("TOK_SAMEDAY"); return TOK_SAMEDAY; }
"schedtime"		{ SCANDBG("TOK_SCHEDTIME"); return TOK_SCHEDTIME; }
"schedule"		{ SCANDBG("TOK_SCHEDULE"); return TOK_SCHEDULE; }
"timezone"|"tz"		{ SCANDBG("TOK_TZ"); return TOK_TZ; }
"to"			{ SCANDBG("TOK_TO"); return TOK_TO; }
"until"			{ SCANDBG("TOK_UNTIL"); return TOK_UNTIL; }
"validfrom"		{ SCANDBG("TOK_VALIDFROM"); return TOK_VALIDFROM; }
"validto"		{ SCANDBG("TOK_VALIDTO"); return TOK_VALIDTO; }
"vartable"		{ SCANDBG("TOK_VARTABLE"); return TOK_VARTABLE; }
[^ #\"\t\r\n\f]+	{ SCANDBG("TOK_STRING");
			  /* Matching any other character... trnf */
			  yylval.string = strdup(yytext);
			  SCANDBG1("TOK_STRING", yytext);
			  return TOK_STRING;
                        }

%%

#ifdef TESTSCAN
/*
\:			{ SCANDBG("TOK_COLON"); return TOK_COLON; }
*/
int main(int argc, char *argv[])
{
    if (argc > 1) {
        yyin = fopen(argv[1], "r");
        fprintf(stderr, "info:%s:using input file '%s'\n", argv[0], argv[1]);
    } else {
        yyin = stdin;
        fprintf(stderr, "info:%s:using standard input file\n", argv[0]);
    }
    fprintf(stderr, "info:%s:starting lexer...\n", argv[0]);
    while(yylex());
    fprintf(stderr, "info:%s:lexer ended...\n", argv[0]);
    return 0;
}
#endif
